package com.example.studentalarm.presenter;

import android.content.Context;

import com.example.studentalarm.bean.EventBusData;
import com.example.studentalarm.bean.TimetableBean;
import com.example.studentalarm.constant.ApiConstant;
import com.example.studentalarm.database.DbManager;
import com.example.studentalarm.utils.StringUtil;
import com.example.studentalarm.utils.Utils;
import com.example.studentalarm.view.IEditTimetableView;

import de.greenrobot.event.EventBus;

/***
 *新增或编辑的P层
 */
public class EditTimetableMvpPresenter extends BasePresenter<IEditTimetableView> {

    public EditTimetableMvpPresenter(Context mContext) {
        super(mContext);
    }


    /***
     * 提交课程表
     * @param timetableBean
     */
    public void commitTimetable(TimetableBean timetableBean) {
        if (StringUtil.isEmpty(timetableBean.getName())) {
            Utils.toast(mContext, "课程名称不能为空！");
        } else if (timetableBean.getTime() == null) {
            Utils.toast(mContext, "上课时间不能为空！");
        } else if (StringUtil.isEmpty(timetableBean.getAddress())) {
            Utils.toast(mContext, "上课地点不能为空！");
        } else {
            timetableBean.setAlarmState(ApiConstant.AlarmState.UNSEND);
            if (timetableBean.getId() == null) {
                DbManager.getInstance().saveTimetable(timetableBean);
                Utils.toast(mContext, "新增成功！");
            } else {
                DbManager.getInstance().updateTimetable(timetableBean);
                Utils.toast(mContext, "修改成功！");
            }
            EventBus.getDefault().post(new EventBusData(ApiConstant.EventBus.REFRESH_OK));
        }

    }

    /**
     * 获取课程表详情
     *
     * @param id 主键ID
     * @return
     */
    public TimetableBean getTimetableBean(Integer id) {
        return DbManager.getInstance().queryTimetableById(id);
    }


}
